package com.fp.m7.client.presenter;

import net.customware.gwt.presenter.client.EventBus;
import net.customware.gwt.presenter.client.place.PlaceRequestEvent;

import com.fp.m7.client.AccountContext;
import com.fp.m7.client.place.HomePlace;
import com.fp.m7.client.place.LoginPlace;
import com.fp.m7.client.place.ProfilePlace;
import com.fp.m7.client.place.RegisterPlace;
import com.fp.m7.shared.services.AccountServiceAsync;
import com.google.inject.Inject;
import com.google.inject.Provider;

/**
 * Defines app's places management, displays a header specific for not authorized user.
 * 
 * @author 7
 */
public class AppPublicPresenter extends AppPresenter<AppPublicPresenter.Display> {

    public interface Display extends AppPresenter.Display {
    }

    @Inject
    public AppPublicPresenter(Display display, EventBus eventBus, AccountContext accountContext, //
            AccountServiceAsync accountService, //
            Provider<HeaderPublicPresenter> headerPublicPresenterProvider, //
            Provider<HeaderSecuredPresenter> headerSecuredPresenterProvider, //
            Provider<LoginPresenter> loginPresenterProvider, //
            Provider<RegisterPresenter> registerPresenterProvider, //
            Provider<HomePresenter> homePresenterProvider, //
            Provider<ProfilePresenter> profilePresenterProvider, //
            Provider<NewCommentPresenter> newCommentPresenterProvider, //
            Provider<TimelinePresenter> timelinePresenterProvider, //
            Provider<SettingsPresenter> settingsPresenterProvider, //
            Provider<InfoPresenter> infoPresenterProvider) {

        super(display, eventBus, accountContext, accountService, headerPublicPresenterProvider,
                headerSecuredPresenterProvider, loginPresenterProvider, registerPresenterProvider,
                homePresenterProvider, profilePresenterProvider, newCommentPresenterProvider,
                timelinePresenterProvider, settingsPresenterProvider, infoPresenterProvider);
    }

    @Override
    public void onPlaceRequest(PlaceRequestEvent event) {
        String placeName = event.getRequest().getName();

        if (LoginPlace.NAME.equals(placeName)) {
            display.setContent(getLoginPresenter().getDisplay());
        } else if (HomePlace.NAME.equals(placeName)) {
            display.setContent(getHomePresenter().getDisplay());
        } else if (RegisterPlace.NAME.equals(placeName)) {
            display.setContent(getRegisterPresenter().getDisplay());
        } else if (ProfilePlace.NAME.equals(placeName)) {
            // in public mode, there is no profile page unless a nickname is provided
            String forUser = event.getRequest().getParameter(ProfilePlace.NICKNAME, null);
            if (forUser != null) {
                display.setContent(getProfilePresenter().getDisplay());
            }
        } else {
            display.setContent(getHomePresenter().getDisplay()); // default
        }
    }

    @Override
    public boolean isForAuthenticatedUser() {
        return false;
    }

    @Override
    protected HeaderPresenter<?> getHeaderPresenter() {
        if (headerPresenter == null) {
            headerPresenter = headerPublicPresenterProvider.get();
            headerPresenter.bind();
        }
        return headerPresenter;
    }
}
